<?php
use Illuminate\Database\Eloquent\SoftDeletingTrait;

class CustomerPayment extends Eloquent
{
	use SoftDeletingTrait;

	public $timestamps   = true;
	protected $dates     = ['deleted_at'];
	protected $table     = 'customer_payment';
	protected $validator = null;

	public function saveData($data, $order = null)
	{
		$customer_payment = new CustomerPayment;

		if($order != null){
			$customer_payment->order = $order->id;
		}else{
			if(isset($data['id'])){
				$customer_payment = CustomerPayment::firstOrCreate(array('id' => $data['id']));
			}
		}

		$date = DateTime::createFromFormat('d-m-Y', $data['date']);
        $date = $date->format("Y-m-d");

		$customer_payment->customer = $data['customer'];
		$customer_payment->type     = $data['type'];
		$customer_payment->total    = $data['total'];
		$customer_payment->date     = $date;

		$customer_payment->save();

		return $customer_payment;
	}

	public function getPayments($order){
		$result = DB::table('customer_payment')->whereType('payment')->whereOrder($order->id)->sum('total');

		return $result;
	}

	public function getCustomerPayments($customer, $date, $order = null){
		$result = DB::table('customer_payment')->whereType('payment')->whereCustomer($customer)->where('date', '<=', $date)->sum('total');

		if($order != null){
			$orderPayment = DB::table('customer_payment')->whereType('payment')->whereCustomer($customer)->where('order', '=', $order)->sum('total');

			$result -= $orderPayment;
		}

		return $result;
	}

	public function getRefunds($order, $date = null){
		$result = DB::table('customer_payment')->whereType('refund')->whereOrder($order->id)->sum('total');

		if($date != null){
			$result = DB::table('customer_payment')->whereType('refund')->whereOrder($order->id)->where('date', '<=', $date)->sum('total');
		}

		return $result;
	}

	public function getCustomerRefunds($order, $date = null){
		$result = DB::table('customer_payment')->whereType('refund')->whereOrder($order->id)->sum('total');

		if($date != null){
			$result = DB::table('customer_payment')->whereType('refund')->whereOrder($order->id)->where('date', '<=', $date)->sum('total');
		}

		return $result;
	}

	public function getDept($customer, $date, $order = null){
		$result = 0;

		$dept = DB::table('customer_payment')
					->whereType('dept')
					->where('date', '<=', $date)
					->whereCustomer($customer)
					->where('deleted_at', '=', null)
					->sum('total');

		$total = DB::table('customer_payment')
					->whereType('total')
					->where('date', '<=', $date)
					->whereCustomer($customer)
					->where('deleted_at', '=', null)
					->sum('total');

		$orderTotal = Order::where('date', '<=', $date)
					->whereCustomer($customer)
					->where('deleted_at', '=', null)
					->sum('total');

		if($order != null){
			$orderTotal -= Order::where('id', '=', $order)
						->where('deleted_at', '=', null)
						->sum('total');
		}

		$result = $dept + $total + $orderTotal;

		return $result;
	}

	public function getPayment($customer, $date, $order = null){
		$result = 0;

		$payment = DB::table('customer_payment')
					->whereType('payment')
					->where('date', '<=', $date)
					->whereCustomer($customer)
					->where('deleted_at', '=', null)
					->sum('total');

		$refund = DB::table('customer_payment')
					->whereType('refund')
					->where('date', '<=', $date)
					->whereCustomer($customer)
					->where('deleted_at', '=', null)
					->sum('total');

		$result = $payment + $refund;

		if($order != null){
			$orderPayment = DB::table('customer_payment')
						->whereType('payment')
						->where('date', '<=', $date)
						->whereOrder($order)
						->where('deleted_at', '=', null)
						->sum('total');

			$orderRefund = DB::table('customer_payment')
						->whereType('refund')
						->where('date', '<=', $date)
						->whereOrder($order)
						->where('deleted_at', '=', null)
						->sum('total');

			$result -= $orderPayment - $orderRefund;
		}

		return $result;
	}

	public function getOrderPayment($order){
		$payment = DB::table('customer_payment')
						->whereType('payment')
						->whereOrder($order)
						->where('deleted_at', '=', null)
						->sum('total');

		return $payment;
	}

	public function getOrderRefund($order){
		$refund = DB::table('customer_payment')
						->whereType('refund')
						->whereOrder($order)
						->where('deleted_at', '=', null)
						->sum('total');
						
		return $refund;
	}

	public function getTotal($customer, $date = null){
		$result = DB::table('customer_payment')->whereType('total')->whereCustomer($customer)->sum('total');

		if($date != null){
			$result = DB::table('customer_payment')->whereType('total')->whereCustomer($customer)->where('date', '<=', $date)->sum('total');
		}

		return $result;
	}

	public function deleteOldPayment($order){
		$result = CustomerPayment::where('order', '=', $order->id)->delete();

		return $result;
	}
}